<?php

/**
 * uniBoard ACP Ban IP
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_ban_ips{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Add language string
		$appCore -> output -> js_vars['l_delete_item_sure'] =  $appCore -> lang['acp_lift_ban_sure'];
		$appCore -> output -> js_vars['l_delete_selected_items_sure'] =  $appCore -> lang['acp_lift_bans_sure'];
			
		// Load fields templates
		$appCore -> templates['form_inputs'] = new tpl_form_inputs( $appCore);
		
		// Delete Ban
		if ( $appCore -> _REQ( 'do') == 'delete')
		{
			// Get Ban API
			$ban_api = new api_ban( $appCore, $appCore -> _REQ( 'ban'));
			
			// Found it?
			if ( $ban_api -> data_loaded && strlen( $ban_api -> ban_ip) > 0)
			{
				// Delete
				$ban_api -> deleteData();
				
				// Set Log
				$appCore -> logAdmin( 'log_admin_banned_ip_lifted', htmlspecialchars( $ban_api -> user_name));
				
				// Draw message
				$appCore -> output -> message( $appCore -> lang['acp_ban_lifted'], $appCore -> lang['acp_ban_ips']);
			}
			else
			{
				// Draw message
				$appCore -> output -> error( $appCore -> lang['acp_ban_not_found'], $appCore -> lang['acp_ban_ips']);
			}
		}
		
		// Delete many Bans
		if ( $appCore -> _POST( 'do') == 'mass_delete')
		{
			// Selected Bans
			$bans = $appCore -> listInput( 'select_ban');
			
			// Anything selected?
			if ( empty( $bans))
			{
				// Draw message
				$appCore -> output -> error( $appCore -> lang['nothing_selected'], $appCore -> lang['acp_ban_ips']);
			}
			else
			{
				// Delete data
				$appCore -> db -> delete( 'ban_filters', 'ban_id IN (' . join( ',', $bans) . ') AND ban_ip <> \'\'');
				
				// Set Log
				$appCore -> logAdmin( 'log_admin_banned_ips_lifted');
				
				// Draw message
				$appCore -> output -> message( $appCore -> lang['acp_bans_lifted'], $appCore -> lang['acp_ban_ips']);
			}
		}
		
		// Cache output
		ob_start();
		
		// New Ban Button
		echo $appCore -> output -> openForm( $appCore -> adminLink( 'act=new_ip_ban'));
		echo $appCore -> output -> openFormBlock( $appCore -> lang['new_ban_ip_form']);
		echo $appCore -> output -> submitButton( $appCore -> lang['new_ban_ip_form_submit']);
		echo $appCore -> output -> closeFormBlock();
		echo $appCore -> output -> closeForm();
		
		// Search Bans
		echo $appCore -> output -> openForm( $appCore -> adminLink( 'act=ban_ips'));
		echo $appCore -> output -> openFormBlock( $appCore -> lang['search_bans_form'], $appCore -> lang['search_bans_form_match']);
		echo $appCore -> output -> formItem( $appCore -> lang['search_bans_form_ip'], $appCore -> drawTextInput( 'name', $appCore -> _REQ( 'name')));
		echo $appCore -> output -> formItem( $appCore -> lang['search_bans_form_reason_admin'], $appCore -> drawTextInput( 'admin', $appCore -> _REQ( 'admin')));
		echo $appCore -> output -> formItem( $appCore -> lang['search_bans_form_reason_user'], $appCore -> drawTextInput( 'user', $appCore -> _REQ( 'user')));
		echo $appCore -> output -> submitButton( $appCore -> lang['search_bans_form_submit']);
		echo $appCore -> output -> closeFormBlock();
		echo $appCore -> output -> closeForm();
		
		// Build search query
		$search_sql = 'ban_ip <> \'\'';
		
		// User Name
		if ( strlen( $appCore -> _REQ( 'name')) > 0)
		{
			$search_sql .= ' AND ban_ip LIKE \'' . str_replace( '*', '%', $appCore -> db -> strEscape( $appCore -> _REQ( 'name'), true)) . '\'';
		}
		
		// Ban Reason - Admin Side
		if ( strlen( $appCore -> _REQ( 'admin')) > 0)
		{
			$search_sql .= ' AND ban_reason_admin LIKE \'' . str_replace( '*', '%', $appCore -> db -> strEscape( $appCore -> _REQ( 'admin'), true)) . '\'';
		}
		
		// Ban Reason - User Side
		if ( strlen( $appCore -> _REQ( 'user')) > 0)
		{
			$search_sql .= ' AND ban_reason_user LIKE \'' . str_replace( '*', '%', $appCore -> db -> strEscape( $appCore -> _REQ( 'user'), true)) . '\'';
		}
		
		// Count Bans to Display
		$bans_number = $appCore -> db -> count( 'ban_filters', $search_sql);
		
		// We will Display 20 logs per page
		$paginator = new class_paginator( $appCore, $appCore -> adminLink( 'act=ban_ips&st=%s&name=' . class_strings::sprintfEnc( $appCore -> _REQ( 'name')) . '&admin=' . class_strings::sprintfEnc( $appCore -> _REQ( 'admin')) . '&user=' . class_strings::sprintfEnc( $appCore -> _REQ( 'user'))), $bans_number, 10);
		
		// Draw Paginator
		$paginator_html = $paginator -> draw();
				
		// Display Paginator
		echo $paginator_html;
		
		// Open table block
		echo $appCore -> output -> openTableBlock( $appCore -> lang['found_bans_form'] . ': ' . $appCore -> number( $bans_number));
				
		// Select Bans
		$appCore -> db -> query( 'SELECT ban_id, ban_ip, ban_reason_admin, ban_date, ban_lenght
		FROM ' . DB_PREFIX . 'ban_filters
		WHERE ' . $search_sql . '
		ORDER BY ban_date DESC
		LIMIT ' . $paginator -> getPosition() . ',10');
		
		// Draw header
		echo $appCore -> output -> tableHeader( array(
			array(
				'left_float width_big',
				$appCore -> lang['column_ip']
			),
			array(
				'right_float width_tiny center',
				( $appCore -> db -> numRows() > 0 ? $appCore -> drawCheckbox( '', false, 'checkbox_master') : '')
			),
			array(
				'right_float width_medium',
				$appCore -> lang['actions']
			),
			array(
				'right_float width_big',
				$appCore -> lang['column_added']
			)
		));		
		
		// Open Mass-Delete forum?
		if ( $appCore -> db -> numRows() > 0)
		{
			// Open form
			echo $appCore -> output -> openForm( $appCore -> adminLink( 'act=ban_ips'));
			echo $appCore -> drawHidden( 'do', 'mass_delete');
		}
		else
		{
			// Draw Row with "no bans found" message
			echo $appCore -> output -> tableRow('
				<div class="cell">
					' . $appCore -> lang['acp_bans_not_found'] . '	
				</div>');
		}
		
		// Parse result
		while ( $ban_result = $appCore -> db -> fetch_assoc())
		{
			// Draw row
			echo $appCore -> output -> tableRow('
				<div class="cell left_float width_quarter">
					' . $appCore -> output -> drawImage( ( $ban_result['ban_lenght'] > 0 && $ban_result['ban_lenght'] < $appCore -> time ? 'state_0' : 'state_1'), $appCore -> lang[( $ban_result['ban_lenght'] > 0 && $ban_result['ban_lenght'] < $appCore -> time ? 'found_bans_expired' : ( $ban_result['ban_lenght'] > 0 ? 'found_bans_active_exp' . $appCore -> getDate( $ban_result['ban_lenght']) : 'found_bans_active'))]) . ' <b>' . htmlspecialchars( $ban_result['ban_ip']) . '</b>' . ( isset( $ban_result['ban_reason_admin'][0]) ? '<div class="item_description">' . nl2br( htmlspecialchars( $ban_result['ban_reason_admin'])) . '</div>' : '') . '
				</div>
				<div class="cell left_border right_float width_tiny center">
					' . $appCore -> drawCheckbox( 'select_ban[' . $ban_result['ban_id'] . ']', false, 'checkbox_children') . '
				</div>
				<div class="cell left_border right_float width_medium center">
					<a href="' . $appCore -> adminLink( 'act=edit_ip_ban&ban=' . $ban_result['ban_id']) . '">' . $appCore -> output -> drawImage( 'action_edit', $appCore -> lang['edit']) . '</a>
					<a href="' . $appCore -> adminLink( 'act=ban_ips&do=delete&ban=' . $ban_result['ban_id']) . '" class="delete_confirm">' . $appCore -> output -> drawImage( 'action_delete', $appCore -> lang['delete']) . '</a>
				</div>
				<div class="cell left_border right_float width_big center">
					' . $appCore -> getDate( $ban_result['ban_date']) . '
				</div>');
		}
		
		// Delete Selected Button?
		if ( $appCore -> db -> numRows() > 0)
		{
			// Draw button
			echo $appCore -> output -> submitButton( $appCore -> lang['delete_selected'], '', 'delete_items_confirm');
			
			// Close form
			echo $appCore -> output -> closeForm();
		}
		
		// Free result
		$appCore -> db -> freeResult();
		
		// Close table block
		echo $appCore -> output -> closeTableBlock();
				
		// Display Paginator
		echo $paginator_html;
		
		// Draw output
		$appCore -> output -> draw( $appCore -> lang['acp_ban_ips'], array( $appCore -> adminLink( 'act=ban_ips') => $appCore -> lang['acp_ban_ips']), ob_get_clean());
				
	}

}